%%%-----------------------------------------------------------------------------
%% @doc chat public API
%% @end
%%%-----------------------------------------------------------------------------

%%%-----------------------------------------------------------------------------
%% 简单说明
%% 1. 私聊频道属于单对单，使用公共进程处理
%% 2. 多人的频道用独立的进程
%% 3. 消息不落地，重启后消息丢失
%% 4. 消息内容为 json，不对消息做任何修改，屏蔽字请在进入之前处理
%% 5. channel manage 管理公共频道，private manage 管理私聊频道
%% 6. 频道名字为 binary 字符串，方便与客户端交互
%% 7. 公共频道没有做合法性检测
%% 8. 消息会分配当前群组内唯一 Id，进入 Message，返回 {Id, Message}
%% 9. 可以通过消息的 Id 来获取之前的消息，Id = 0 为最新消息
%%%-----------------------------------------------------------------------------

-module(chat).

-export([ start/0
        , stop/0
        ]).

-export([ channel_name/2 ]).

-export([ public_login/2
        , public_login/3
        , public_logout/2
        , public_logout/3
        , public_talk/2
        , public_talk/3
        , public_fetch/2
        , public_fetch/3
        ]).

-export([ private_login/2
        , private_logout/2
        , private_talk/2
        , private_fetch/2
        ]).


%%%-----------------------------------------------------------------------------
start() ->
    %{ok, _} = application:ensure_all_started(jsx),
    {ok, _} = application:ensure_all_started(chat).

stop() ->
    %% 停止注册监听
    %% 删除所有已注册监听
    ok.

%%%-----------------------------------------------------------------------------
%% 频道名字
channel_name(Kind, Id) ->
    list_to_binary(lists:concat([Kind, Id])).

%%%-----------------------------------------------------------------------------
%% 公共频道
%-spec public_login(ChannelName::BinString, Subscriber::pid) -> ok.
public_login(ChannelName, Subscriber) ->
    chat_channel_manage:login(ChannelName, Subscriber).
public_login(Kind, Id, Subscriber) ->
    ChannelName = channel_name(Kind, Id),
    public_login(ChannelName, Subscriber).

%-spec public_logout(ChannelName::BinString, Subscriber::pid)-> ok.
public_logout(ChannelName, Subscriber) ->
    chat_channel_manage:logout(ChannelName, Subscriber).
public_logout(Kind, Id, Subscriber) ->
    ChannelName = channel_name(Kind, Id),
    public_logout(ChannelName, Subscriber).

%-spec public_talk(ChannelName::BinString, Message::term) -> ok.
public_talk(ChannelName, Message) ->
    chat_channel_manage:talk(ChannelName, Message).    
public_talk(Kind, Id, Message) ->
    ChannelName = channel_name(Kind, Id),
    public_talk(ChannelName, Message).

%-spec public_fetch(ChannelName::BinString, MsgId::integer) -> MsgList::[{MsgId, Msg}].
public_fetch(ChannelName, MsgId) ->
    chat_channel_manage:fetch(ChannelName, MsgId).
public_fetch(Kind, Id, MsgId) ->
    ChannelName = channel_name(Kind, Id),
    public_fetch(ChannelName, MsgId).

%%%-----------------------------------------------------------------------------
%% 私聊
%-spec private_login(RoleId::integer, Subscriber::pid) -> ok.
private_login(RoleId, Subscriber) ->
    chat_private_manage:login(RoleId, Subscriber).

%-spec private_logout(RoleId::integer, Subscriber::pid) -> ok.
private_logout(RoleId, Subscriber) ->
    chat_private_manage:logout(RoleId, Subscriber).

%-spec private_talk(ToRoleId::integer, Message::term) -> ok.
private_talk(ToRoleId, Message) ->
    chat_private_manage:talk(ToRoleId, Message).

%-spec public_fetch(ChannelName::BinString, MsgId::integer) -> MsgList::[{MsgId, Msg}].
private_fetch(RoleId, MsgId) ->
    chat_private_manage:fetch(RoleId, MsgId).


%% internal functions

